package org.dna.metronomo.persistence.impl;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;
import org.dna.metronomo.persistence.IActivityDAO;
import org.dna.metronomo.persistence.PersistenceException;
import org.dna.metronomo.persistence.impl.annotations.SessionField;
import org.dna.metronomo.persistence.impl.annotations.SessionMethod;
import org.dna.mybatis.criteria.Criteria;
import org.tigris.scarab.mapper.ActivityMapper;
import org.tigris.scarab.model.Activity;

/**
 * @composed 1 - 1 IBatisMetronomoDAO
 */
public class IBatisActivityDAO implements IActivityDAO {

    private static final Logger LOG = Logger.getLogger(IBatisActivityDAO.class);
    
    @SessionField
    SqlSession m_session;

//    @InSession(sessionField=m_session)
//    public List<Activity> allActivities() throws PersistenceException {
//        ActivityMapper mapper = m_session.getMapper(ActivityMapper.class);
//        return mapper.listAll();
//    }

    @SessionMethod
    public List<Activity> allActivities() throws PersistenceException {
        ActivityMapper mapper = m_session.getMapper(ActivityMapper.class);
        return mapper.listAll();
    }

    @SessionMethod
    public List<Activity> queryActivityByExample(final Criteria criteria)
            throws PersistenceException {
        if (!criteria.verify()) {
            return null;
        }
        criteria.bind(m_session.getConfiguration());
        ActivityMapper mapper = m_session.getMapper(ActivityMapper.class);
        return mapper.selectByExample(criteria);
    }

    @SessionMethod
    public Activity queryActivityByID(final long id) {
        ActivityMapper mapper = m_session.getMapper(ActivityMapper.class);
        return mapper.selectByID(id);
    }

    @SessionMethod
    public int countByExample(final Criteria criteria) throws PersistenceException {
        ActivityMapper mapper = m_session.getMapper(ActivityMapper.class);
        return mapper.countByExample(criteria);
    }
}
